﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;


namespace Portal.Web.Areas.Admin.Controllers
{
    public sealed class SettingController : BaseAdminController
    {
        /// <summary>
        /// 获取设置数据仓库
        /// </summary>
        private readonly ISettingRepository _settingRepository;
        /// <summary>
        /// 获取当语言前设置
        /// </summary>
        private readonly ISetting _setting;
        private readonly IList<ISetting> _settingList;
        public SettingController(ISettingRepository settingRepository)
        {
            _settingList = SettingCache.GetList();
            _settingRepository = settingRepository;
            var seting = _settingList.FirstOrDefault(o => o.Culture == Culture) ?? _settingRepository.CreateEntity();
            seting.Culture = Culture;
            _setting = seting;

        }

        public ActionResult Index()
        {
            return View(_setting);
        }
        /// <summary>
        /// 站点设置
        /// </summary>
        /// <returns></returns>
        [Description("站点设置")]
        public ActionResult Site()
        {
            return View(_setting);
        }
        [HttpPost]
        [ValidateInput(false)]
        [CacheResetFilterAttribute(CacheKey.SettingList)]
        public ActionResult Site(FormCollection formCollection, Setting entity)
        {
            _setting.Title = entity.Title;
            _setting.MetaKeywords = entity.MetaKeywords;
            _setting.MetaDescription = entity.MetaDescription;
            _setting.Telnet = entity.Telnet;
            _setting.HotLineTelnet = entity.HotLineTelnet;
            _setting.Fax = entity.Fax;
            _setting.CompanyAdress = entity.CompanyAdress;
            _setting.Email = entity.Email;
            _setting.Qq = entity.Qq;
            _setting.CustomerServiceTelnet = entity.CustomerServiceTelnet;
            _setting.AccessCountHtml = entity.AccessCountHtml;
            _setting.Mobile = entity.Mobile;
            _setting.Copyright = entity.Copyright;
            _setting.OrderCompanyName = entity.OrderCompanyName;
            _setting.OrderEmail = entity.OrderEmail;
            _setting.Mobile = entity.Mobile;
            _setting.Contacter = entity.Contacter;
            _setting.Code = entity.Code;
            SettingAddOrUpdate();
            return View(_setting);
        }
        public ActionResult Commany()
        {
            return View(_setting);
        }
        public ActionResult ContactUs()
        {
            return View(_setting);
        }
        [HttpPost]
        [ValidateInput(false)]
        [CacheResetFilterAttribute(CacheKey.SettingList)]
        public ActionResult ContactUs(FormCollection formCollection, string contactUs)
        {

            _setting.ContactUs = contactUs;
            SettingAddOrUpdate();
            return View(_setting);
        }
        public ActionResult About()
        {
            return View(_setting);
        }
        [HttpPost]
        [ValidateInput(false)]
        [CacheResetFilterAttribute(CacheKey.SettingList)]
        public ActionResult About(FormCollection formCollection, string about, string companyProfile)
        {

            _setting.About = about;
            _setting.CompanyProfile = companyProfile;
            SettingAddOrUpdate();
            return View(_setting);
        }
        public ActionResult SalesNetwork()
        {
            return View(_setting);
        }
        [HttpPost]
        [ValidateInput(false)]
        [CacheResetFilterAttribute(CacheKey.SettingList)]
        public ActionResult SalesNetwork(FormCollection formCollection, string salesNetwork)
        {
            _setting.SalesNetwork = salesNetwork;
            SettingAddOrUpdate();
            return View(_setting);


        }
        public ActionResult Recruitment()
        {
            return View(_setting);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="formCollection"></param>
        /// <param name="recruitment"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        [CacheResetFilterAttribute(CacheKey.SettingList)]
        public ActionResult Recruitment(FormCollection formCollection, string recruitment)
        {
            _setting.Recruitment = recruitment;
            SettingAddOrUpdate();
            return View(_setting);
        }
        /// <summary>
        /// 保存更新Setting转变
        /// </summary>
        private void SettingAddOrUpdate()
        {
            _settingRepository.AddOrUpdate(_setting);
            _settingRepository.Save();
        }

        public ActionResult Video()
        {
            return View(_setting);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formCollection"></param>
        /// <param name="video"> </param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        [CacheResetFilterAttribute(CacheKey.SettingList)]
        public ActionResult Video(FormCollection formCollection, string video)
        {
            _setting.Video = video;
            SettingAddOrUpdate();
            return View(_setting);
        }


        /// <summary>
        /// Kndeditor文件上传
        /// </summary>
        /// <returns></returns>
        public void UploadSettingArticleJson()
        {
            var kndeditor = new KndeditorHelper(System.Web.HttpContext.Current, "Setting", CurrentUserName);
            kndeditor.UploadJson();
        }
        /// <summary>
        /// Kndeditor文件管理
        /// </summary>
        public void FileManagerSettingJson()
        {
            var kndeditor = new KndeditorHelper(System.Web.HttpContext.Current, "Setting", CurrentUserName);
            kndeditor.FileManagerJson();
        }
    }
}
